package com.gdpu.controller;

import com.gdpu.api.CommonPage;
import com.gdpu.api.CommonResult;
import com.gdpu.model.PmsCategory;
import com.gdpu.service.PmsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/3/20
 * 商品分类模块
 */
@Api( tags = "商品分类")
@RestController
public class PmsCategoryController {

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @ApiOperation("获取商品分类")
    @GetMapping("/categorys/")
    public CommonResult list(@RequestParam(value = "pageNum",defaultValue = "0") int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        CommonPage list = pmsCategoryService.list(pageNum, pageSize);
        return  CommonResult.success(list);

    }

    @ApiOperation("获取该商品分类下的商品列表")
    @GetMapping("/categorys/goods/{id}")
    public CommonResult listByCategoryId(@RequestParam(value = "pageNum",defaultValue = "0") int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                       @PathVariable(value = "id") Integer id){
        CommonPage list = pmsCategoryService.listByParentId(id,pageNum, pageSize);
        return  CommonResult.success(list);

    }

    @ApiOperation("添加商品分类")
    @PostMapping("/categorys")
    public CommonResult add(@RequestBody PmsCategory pmsCategory){
         int count = pmsCategoryService.add(pmsCategory);
         if(count != 0){
             return CommonResult.success();
         }
         return  CommonResult.failed();
    }

    @ApiOperation("更新商品分类")
    @PutMapping("/categorys")
    public CommonResult update(@RequestBody PmsCategory pmsCategory){
        int count = pmsCategoryService.update(pmsCategory);
        if (count != 0){
            return  CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除商品分类")
    @DeleteMapping("/categorys/{id}")
    public CommonResult delete(@PathVariable("id")Integer id){
        int count = pmsCategoryService.delete(id);
        if (count != 0){
            return  CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据父类id获取子分类")
    @GetMapping("/categorys/getByPid")
    public CommonResult get(@RequestParam(name = "id",required = false)Integer id){
        List<PmsCategory> categorys = pmsCategoryService.getCategorysById(id);

        if (categorys!=null){
            HashMap<Object, Object> map = new HashMap<>(1);
            map.put("categoryList",categorys);
            return CommonResult.success(map);
        }
        return CommonResult.failed();
    }




}
